import { createWebHashHistory, createRouter } from "vue-router";
import { storeToRefs } from "pinia";
import { useEmpStore } from "../store/emp";
import { ElMessage } from "element-plus";

// import HomeView from "./HomeView.vue";
// import AboutView from "./AboutView.vue";

const routes = [
  {
    path: "/loginA",
    component: () => import("../views/LoginA.vue"),
    meta: { requiresAuth: false },
  },
  {
    path: "/loginE",
    component: () => import("../views/LoginE.vue"),
    meta: { requiresAuth: false },
  },
  {
    path: "/resetPassword",
    component: () => import("../views/ReSetPassword.vue"),
    meta: { requiresAuth: false },
  },
  { 
    path: '/', 
    component: () => import('../views/Home.vue'),
    meta: { requiresAuth: true }, 
    children: [
      {
        path: "/main",
        component: () => import("../views/Main.vue"),
        meta: { requiresAuth: true },
      },
      {
        path: "/empinfo",
        component: () => import("../views/Empinfo.vue"),
        meta: { requiresAuth: true },
      },
      {
        path: "/changePassword",
        component: () => import("../views/ChangePassword.vue"),
        meta: { requiresAuth: true },
      },
      {
        path: "/attend",
        component: () => import("../views/Attend.vue"),
        meta: { requiresAuth: true },
      },
      {
        path: "/retrospective",
        component: () => import("../views/Retrospective.vue"),
        meta: { requiresAuth: true },
      },
      {
        path: "/approveAttend",
        component: () => import("../views/ApproveAttend.vue"),
        meta: { requiresAuth: true },
      },
      {
        path: "/app4vocation",
        component: () => import("../views/App4vocation.vue"),
        meta: { requiresAuth: true },
      },
      {
        path: "/vocationlist",
        component: () => import("../views/Vocationlist.vue"),
        meta: { requiresAuth: true },
      },
      {
        path: "/approveVocation",
        component: () => import("../views/ApproveVocation.vue"),
        meta: { requiresAuth: true },
      },
      {
        path: "/approverlist",
        component: () => import("../views/Approverlist.vue"),
        meta: { requiresAuth: true },
      },
      {
        path: "/empworktype",
        component: () => import("../views/Empworktype.vue"),
        meta: { requiresAuth: true },
      },
      {
        path: "/worktype",
        component: () => import("../views/Worktype.vue"),
        meta: { requiresAuth: true },
      },
      {
        path: "/vocationtype",
        component: () => import("../views/Vocationtype.vue"),
        meta: { requiresAuth: true },
      },
      {
        path: "/emplist",
        component: () => import("../views/Emplist.vue"),
        meta: { requiresAuth: true },
      },
      {
        path: "/deptlist",
        component: () => import("../views/Deptlist.vue"),
        meta: { requiresAuth: true },
      },
      {
        path: "/position",
        component: () => import("../views/Position.vue"),
        meta: { requiresAuth: true },
      },
    ]
  }
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach((to, from) => {
  // 而不是去检查每条路由记录
  to.matched.some(record => record.meta.requiresAuth)

  let store = useEmpStore()
  let { empInfo } = storeToRefs(store)
  let auth_required = to.meta.requiresAuth
  
  if(!auth_required){
    return true
  }
  else if (auth_required && empInfo.value) {
    return true
  }
  else {
    ElMessage.error('请先登录')
    return '/loginE'
  }
})




export default router;
